package com.facebook.common.internal;

import javax.annotation.Nullable;

public final class Preconditions {
    private Preconditions() {

    }

    private static String badElementIndex(int arg5, int arg6, @Nullable String arg7) {
        String v0;
        Object[] v1;
        int v4 = 2;
        if(arg5 < 0) {
            v1 = new Object[v4];
            v1[0] = arg7;
            v1[1] = Integer.valueOf(arg5);
            v0 = Preconditions.format("%s (%s) must not be negative", v1);
        }
        else if(arg6 < 0) {
            throw new IllegalArgumentException("negative size: " + arg6);
        }
        else {
            v1 = new Object[3];
            v1[0] = arg7;
            v1[1] = Integer.valueOf(arg5);
            v1[v4] = Integer.valueOf(arg6);
            v0 = Preconditions.format("%s (%s) must be less than size (%s)", v1);
        }

        return v0;
    }

    private static String badPositionIndex(int arg5, int arg6, @Nullable String arg7) {
        String v0;
        Object[] v1;
        int v4 = 2;
        if(arg5 < 0) {
            v1 = new Object[v4];
            v1[0] = arg7;
            v1[1] = Integer.valueOf(arg5);
            v0 = Preconditions.format("%s (%s) must not be negative", v1);
        }
        else if(arg6 < 0) {
            throw new IllegalArgumentException("negative size: " + arg6);
        }
        else {
            v1 = new Object[3];
            v1[0] = arg7;
            v1[1] = Integer.valueOf(arg5);
            v1[v4] = Integer.valueOf(arg6);
            v0 = Preconditions.format("%s (%s) must not be greater than size (%s)", v1);
        }

        return v0;
    }

    private static String badPositionIndexes(int arg4, int arg5, int arg6) {
        String v0;
        if(arg4 < 0 || arg4 > arg6) {
            v0 = Preconditions.badPositionIndex(arg4, arg6, "start index");
        }
        else {
            if(arg5 >= 0 && arg5 <= arg6) {
                v0 = Preconditions.format("end index (%s) must not be less than start index (%s)", new Object[]{Integer.valueOf(arg5), Integer.valueOf(arg4)});
                return v0;
            }

            v0 = Preconditions.badPositionIndex(arg5, arg6, "end index");
        }

        return v0;
    }

    public static void checkArgument(boolean arg1) {
        if(!arg1) {
            throw new IllegalArgumentException();
        }
    }

    public static void checkArgument(boolean arg2, @Nullable Object arg3) {
        if(!arg2) {
            throw new IllegalArgumentException(String.valueOf(arg3));
        }
    }

    public static void checkArgument(boolean arg2, @Nullable String arg3, @Nullable Object[] arg4) {
        if(!arg2) {
            throw new IllegalArgumentException(Preconditions.format(arg3, arg4));
        }
    }

    public static int checkElementIndex(int arg1, int arg2) {
        return Preconditions.checkElementIndex(arg1, arg2, "index");
    }

    public static int checkElementIndex(int arg2, int arg3, @Nullable String arg4) {
        if(arg2 >= 0 && arg2 < arg3) {
            return arg2;
        }

        throw new IndexOutOfBoundsException(Preconditions.badElementIndex(arg2, arg3, arg4));
    }

    public static <T> T checkNotNull(T t) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException();
    }


    public static <T> T checkNotNull(T t, @Nullable Object obj) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException(String.valueOf(obj));
    }

    public static <T> T checkNotNull(T t, @Nullable String str, @Nullable Object... objArr) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException(format(str, objArr));
    }


    public static int checkPositionIndex(int arg1, int arg2) {
        return Preconditions.checkPositionIndex(arg1, arg2, "index");
    }

    public static int checkPositionIndex(int arg2, int arg3, @Nullable String arg4) {
        if(arg2 >= 0 && arg2 <= arg3) {
            return arg2;
        }

        throw new IndexOutOfBoundsException(Preconditions.badPositionIndex(arg2, arg3, arg4));
    }

    public static void checkPositionIndexes(int arg2, int arg3, int arg4) {
        if(arg2 >= 0 && arg3 >= arg2 && arg3 <= arg4) {
            return;
        }

        throw new IndexOutOfBoundsException(Preconditions.badPositionIndexes(arg2, arg3, arg4));
    }

    public static void checkState(boolean arg1) {
        if(!arg1) {
            throw new IllegalStateException();
        }
    }

    public static void checkState(boolean arg2, @Nullable Object arg3) {
        if(!arg2) {
            throw new IllegalStateException(String.valueOf(arg3));
        }
    }

    public static void checkState(boolean arg2, @Nullable String arg3, @Nullable Object[] arg4) {
        if(!arg2) {
            throw new IllegalStateException(Preconditions.format(arg3, arg4));
        }
    }

    static String format(@Nullable String arg7, @Nullable Object[] arg8) {
        int v0 = 0;
        String v2 = String.valueOf(arg7);
        StringBuilder v3 = new StringBuilder(v2.length() + arg8.length * 16);
        int v1 = 0;
        while(v0 < arg8.length) {
            int v4 = v2.indexOf("%s", v1);
            if(v4 == -1) {
                break;
            }

            v3.append(v2.substring(v1, v4));
            v3.append(arg8[v0]);
            v1 = v4 + 2;
            ++v0;
        }

        v3.append(v2.substring(v1));
        if(v0 < arg8.length) {
            v3.append(" [");
            v3.append(arg8[v0]);
            ++v0;
            while(v0 < arg8.length) {
                v3.append(", ");
                v3.append(arg8[v0]);
                ++v0;
            }

            v3.append(']');
        }

        return v3.toString();
    }
}

